import { getStorage } from '@wowon/utils';

import { STORAGE_NAMES } from '@/common/constant';
import reqTypes from '@/common/shared/req-types';
import { refreshToken as refreshTokenApi } from '@/services/login';
import logout from '@/utils/logout';
import { message } from '@/sys';

import serverPath from './server-path';
import abort from './abort';
import withHeader from './with-header';
import format from './format';
import refreshToken from './refresh-token';

export default {
  getHeaders() {
    return {
      accept: 'application/json;charset=UTF-8',
      authorization: getStorage(STORAGE_NAMES.token),
    };
  },
  requestInterceptors: [serverPath, abort.request, withHeader, format.request],
  responseInterceptors: [
    abort.response,
    format.response({
      reqTypes,
      onMessage: (msg, pass) => {
        message[pass ? 'success' : 'error'](msg);
      },
    }),
    refreshToken.response({
      refresh: refreshTokenApi,
      onFail: logout,
    }),
  ],
};
